﻿using MiniFox.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WebApi.Test
{
    public class OrderServiceTester
    {
        static string currentId;

        static OrderItemDto RandomOrderItem()
        {
            return new OrderItemDto
            {
                goodsName = Tools.GenerateRandomCode(20),
                qty = new Random().Next(1, 10),
                unit = "个",
                unitPrice = (decimal)((new Random().Next(1000, 1500)) * 0.01d)
            };
        }
        public static void Create()
        {
            string uri = $"{UriExtensions.DS_URI}/order/create";
            var requestObj = new OrderDto
            {
                orderId = string.Empty,
                buyer = Tools.GenerateRandomCode(20),
                seller = Tools.GenerateRandomCode(10),
                payMode = Tools.GenerateRandomCode(20)
            };
            requestObj.items.Add(RandomOrderItem());
            requestObj.items.Add(RandomOrderItem());
            var result = uri.PostAsync<OrderDto, Response<OrderVo>>(requestObj).Result;

            currentId = result.data.orderId;
        }

        public static void Update()
        {
            string uri = $"{UriExtensions.DS_URI}/order/update";
            var requestObj = new OrderDto
            {
                orderId = currentId,
                buyer = Tools.GenerateRandomCode(20),
                seller = Tools.GenerateRandomCode(10),
                payMode = Tools.GenerateRandomCode(20),
            };
            requestObj.items.Add(RandomOrderItem());
            requestObj.items.Add(RandomOrderItem());
            var result = uri.PostAsync<OrderDto, Response<OrderVo>>(requestObj).Result;
        }
        public static void Pay()
        {
            string uri = $"{UriExtensions.DS_URI}/order/pay";

            var responseObj = uri.PostAsync<string, Response<OrderVo>>(currentId).Result;
        }
        public static void Delete()
        {
            string uri = $"{UriExtensions.DS_URI}/order/delete";

            var responseObj = uri.PostAsync<string, Response<string>>(currentId).Result;
        }

        public static void GetById()
        {
            Console.WriteLine("请输入要查询的订单id");
            string uri = $"{UriExtensions.DS_URI}/order/get?orderId={Console.ReadLine()}";
            var responseObj = uri.GetAsync<Response<OrderVo>>().Result;
        }

        public static void Query()
        {
            string uri = $"{UriExtensions.DS_URI}/order/query";
            Console.WriteLine("请输入要查询关键字(匹配订单号)");

            string keyword = Console.ReadLine();
            var requestObj = new
            {
                sortField = "",
                sortDesc = false,
                pageIndex = 2,
                pageSize = 5,
                parameters = new
                {
                    keyword = keyword
                }
            };
            var json = uri.PostAsync(JsonConvert.SerializeObject(requestObj)).Result;

        }
    }

    public class OrderDto
    {

        public string orderId { get; set; }

        public string buyer { get; set; }

        public string seller { get; set; }

        public decimal totalAmt
        {
            get
            {
                decimal total = 0;
                foreach (var item in this.items)
                {
                    total += item.qty * item.unitPrice;
                }
                return total;
            }
        }

        public decimal tradeAmt { get
            {
                return this.totalAmt * (decimal)0.9;
            }
        }

        public string payMode { get; set; }

        public List<OrderItemDto> items { get; set; } = new List<OrderItemDto>();
    }

    public class OrderItemDto
    {
        public string goodsName { get; set; }

        public string unit { get; set; }

        public int qty { get; set; }

        public decimal unitPrice { get; set; }

        public float taxRate { get; set; }
    }

    public class OrderVo
    {
        public string orderId { get; set; }

        public string buyer { get; set; }

        public string seller { get; set; }

        public decimal totalAmt { get; set; }

        public decimal tradeAmt { get; set; }

        public bool paid { get; set; }

        public string payMode { get; set; }

        public List<OrderItemVo> items { get; set; } = new List<OrderItemVo>();
    }

    public class OrderItemVo
    {
        public int ordinal { get; set; }

        public string goodsName { get; set; }

        public string unit { get; set; }

        public int qty { get; set; }

        public decimal unitPrice { get; set; }

        public decimal tradeAmt { get; set; }

        public float taxRate { get; set; }

        public decimal taxFee { get; set; }
    }
}
